<?php

namespace app\models;

use common\helpers\Paginator;
use Yii;
use yii\behaviors\OptimisticLockBehavior;
use yii\db\Query;

/**
 * This is the model class for table "menu".
 *
 * @property int $id
 * @property string $name
 * @property int|null $parent
 * @property string|null $route
 * @property int|null $order
 * @property string|null $data
 * @property int|null $status
 *
 * @property Menu $parent0
 * @property Menu[] $menus
 */
class Menu extends \yii\db\ActiveRecord
{


    public function behaviors()
    {
        return [
            OptimisticLockBehavior::className(),
        ];
    }

    public function optimisticLock()
    {
        return 'version';
    }

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'menu';
    }

    /**
     * Notes: 使用其他数据库进行
     * User: Administrator
     * DateTime: 2021/4/1 16:55
     * @return mixed|\yii\db\Connection
     */
//    public static function getDb()
//    {
//        return Yii::$app->db2;
//    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id'], 'required'],
            [['name'], 'required'],
            [['parent', 'order', 'status'], 'integer'],
            [['data'], 'string'],
            [['name'], 'string', 'max' => 128],
            [['route'], 'string', 'max' => 256],
            [['parent'], 'exist', 'skipOnError' => true, 'targetClass' => Menu::className(), 'targetAttribute' => ['parent' => 'id']],
            [['version'], 'integer'],

        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id'     => Yii::t('app', 'ID'),
            'name'   => Yii::t('app', 'Name'),
            'parent' => Yii::t('app', 'Parent'),
            'route'  => Yii::t('app', 'Route'),
            'order'  => Yii::t('app', 'Order'),
            'data'   => Yii::t('app', 'Data'),
            'status' => Yii::t('app', 'Status'),
            'version' => Yii::t('app', 'Version'),
        ];
    }

    /**
     * Gets query for [[Parent0]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getParent0()
    {
        return $this->hasOne(Menu::className(), ['id' => 'parent']);
    }

    /**
     * Gets query for [[Menus]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getMenus()
    {
        return $this->hasMany(Menu::className(), ['parent' => 'id']);
    }


    /**
     * Notes: 自定义筛选项
     * User: Administrator
     * DateTime: 2021/4/2 14:29
     * @param $attribute
     * @param $params
     */
    public function customRequired($attribute, $params)
    {
        if (empty($this->data)){
            $this->addError($attribute, '必填项');
        }
    }


    /**
     * Notes: 分页
     * User: Administrator
     * DateTime: 2021/4/2 17:14
     * @return Paginator
     */
    public static function pager()
    {
        $query = new Query();
        $query->from(Menu::tableName());
        $paginator = Paginator::getInstance();
        $paginator->pagination($query);
        return $paginator;
    }
}
